import BaseView from "../../components/BaseView";
import cjs=createjs
import UIComponent from "../../components/UIComponent";
import Global from "../../Global";
import Bullet from "../../components/Bullet";
import Pool from "../../Pool";
import Enemy from "../../components/Enemy";
import Bomb from "../../components/Bomb";
//import createjs from "../../../createjs";
//
class Background extends UIComponent {
    private img0:cjs.Bitmap
    private img1:cjs.Bitmap
    private top=0
    constructor() {
        super()
        const lib=Global.getLibrary()
        this.img0=new lib.BgBitmap()
        this.addChild(this.img0)
        this.img1=new lib.BgBitmap()
        this.addChild(this.img1)
        //this.reflush()
    }
    run():void{
        this.on('tick',this.onEnterFrame)
    }
    stop():void{
        this.off('tick',this.onEnterFrame)
    }
    private onEnterFrame():void{
        this.top+=2
        if (this.top >= this.height) this.top = this.top%this.height
        this.reflush()
    }
    reflush(): void {
        const rect=this.img0.getBounds()
        this.img0.setTransform(0,this.top,this.width/rect.width,this.height/rect.height)
        this.img1.setTransform(0,-this.height+this.top+1.5,this.width/rect.width,this.height/rect.height)
    }
}
export default class MainView extends BaseView{
    private _bg=new Background()
    private _hero
    private offsetP=new cjs.Point(0,0)
    private frame=0
    constructor(w?:number,h?:number){
        super(w,h)
        this.name='MainView'
        this._bg.setSize(this.width,this.height)
        this.addChild(this._bg)
        this._bg.run()

        const lib=Global.getLibrary()
        this._hero=new lib.Hero()
        this._hero.bullet0.visible=false
        this._hero.bullet1.visible=false
        const rect=this._hero.getBounds()
        this._hero.setTransform((this.width)/2,this.height-rect.height-200)
        this.addChild(this._hero)

        //
        //this.on('pressmove',this.onPressMove)
        wx.onTouchStart(this.onTouchStart.bind(this))
        wx.onTouchMove(this.onTouchMove.bind(this))
        this.on('tick',this.onTick)
        this.reflush()
        //bgm
        const audio=this.getResource('bgm') as HTMLAudioElement
        audio.loop=true
        audio.currentTime=0
        audio.play()
    }
    private onTouchStart(res:any):void{
        //console.log(res)
        const {clientX,clientY}=res.touches[0]
        const x=clientX/Global.scale
        const y=clientY/Global.scale
        this.offsetP.x=this._hero.x-x
        this.offsetP.y=this._hero.y-y
    }
    private onTouchMove(res:any):void{
        //console.log(res.changedTouches[0])
        //const {clientX,clientY}=res.changedTouches[0]
        const {clientX,clientY}=res.touches[0]
        //console.log(clientX/Global.scale,clientY/Global.scale)
        const x=clientX/Global.scale
        const y=clientY/Global.scale
        this._hero.x=x+this.offsetP.x
        this._hero.y=y+this.offsetP.y
    }
    private onTick():void{
        this.frame++
        if(this.frame%20==0){
            //const scale=0.7
            const d=30
            const rect=this._hero.getBounds()
            const pool=Pool.getInstance()

            let bullet=pool.getItemByClass('Bullet',Bullet)
            bullet.recover=false
            bullet.setTransform(this._hero.x+this._hero.bullet0.x,this._hero.y-30)
            this.addChild(bullet)
            bullet.move()

            bullet=pool.getItemByClass('Bullet',Bullet)
            bullet.recover=false
            bullet.setTransform(this._hero.x+this._hero.bullet1.x,this._hero.y-30)
            this.addChild(bullet)
            bullet.move()
            //子弹音效
            const audio=this.getResource('bullet') as HTMLAudioElement
            audio.currentTime=0
            audio.play()
        }
        if(this.frame%30==0){
            const pool=Pool.getInstance()
            const enemy=pool.getItemByClass('Enemy',Enemy)
            enemy.recover=false
            enemy.setTransform(Math.random()*(this.width-enemy.width),-enemy.height)
            this.addChild(enemy)
            enemy.move()
        }
        if(this.frame>=60){
            this.frame=0
        }
        this.checkHit()
    }
    reflush(): void {
        //throw new Error("Method not implemented.");
    }
    checkHit():void{
        const pool=Pool.getInstance()
        const list=this.children
        const len=list.length
        //let count=0
        loop:for(let i=0;i<len;i++){
            const enemy=list[i]
            if(enemy.name!='enemy')continue
            //console.log(i,enemy.name)
            for(let j=0;j<len;j++){
                const bullet=list[j]
                if(bullet.name!='bullet')continue
                //console.log(j,bullet.name)
                if(this.myHitTest(enemy,bullet.x,bullet.y)){
                    //console.log('命中飞机');
                    //命中飞机
                    const bomb=pool.getItemByClass('Bomb',Bomb);
                    bomb.setTransform(enemy.x+60,enemy.y+79/2);
                    this.addChild(bomb);
                    bomb.reset();
                    (bullet as Bullet).recover=true;
                    (enemy as Enemy).recover=true;
                    //爆炸音效
                    const audio=this.getResource('boom') as HTMLAudioElement
                    audio.currentTime=0
                    audio.play()
                    //
                    this.dispatchEvent('hit_enemy')
                    break loop;
                }
                //count++
            }
        }
        //console.log('碰撞检测'+count)
    }
    myHitTest(obj:cjs.DisplayObject,x:number,y:number):boolean{
        const rect=obj.getBounds()
        const w=rect.width
        const h=rect.height
        if(y>obj.y&&y<(obj.y+h)){
            if(x>obj.x&&x<(obj.x+w)){
                return true
            }
        }
        return false
    }
}